//
// Created by 黄河 on 2024/1/16.
//
#include "operate_hdfs.h"

#include <stdio.h>

operatehdfs::operatehdfs() {

}

void operatehdfs::operatehdfs_conn(char const *ip, int port)
{
    fs = hdfsConnect(ip, port);
    if (!fs) {
        printf("connect hdfserror!\n");
    }
}

void operatehdfs::operatehdfs_open(char *filename) {
    writeFile = hdfsOpenFile(fs, filename, O_WRONLY | O_CREAT, 0, 0, 0);
    if (NULL == writeFile) {
        printf("failed fs:%p open%s for writing !\n", fs, filename);
    }
}

int operatehdfs::operatehdfs_write(unsigned char *buff, int bufflen) {
    if (NULL== writeFile){
        return 0;
    }

    int num_written_bytes = hdfsWrite(fs, writeFile, (void *) buff, bufflen);
    if (hdfsFlush(fs, writeFile)) {
        printf("failed toflush\n");
        return 0;
    }
    return num_written_bytes;
}

void operatehdfs::operatehdfs_close() {
    if (NULL != writeFile){
        hdfsCloseFile(fs, writeFile);
    }
}

void operatehdfs::operatehdfs_rename(const char* oldPath,const char *newPath) {
    int ret = 0;
    ret = hdfsRename(fs, oldPath, newPath);
}

